package com.nbgroup.service;

import com.nbgroup.dao.LiteratureChildRepositoty;
import com.nbgroup.dao.LiteratureRepositoty;
import com.nbgroup.entity.Literature;
import com.nbgroup.entity.LiteratureChild;
import com.nbgroup.utils.QueryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class LiteratureChildService {

    @Autowired
    private LiteratureChildRepositoty literatureChildRepositoty;
    @Autowired
    private QueryUtil queryUtil;

    /**
     * 查询 指定展厅 的展位
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    public List<LiteratureChild> findAllForLiteratureChild(Integer literatureId){
        String sql = "SELECT * FROM `literaturechild` WHERE literature_id = '"+literatureId+"' AND sts = '1'";
        List<LiteratureChild> literatureChildList = queryUtil.query(sql, LiteratureChild.class);
        return literatureChildList;
    }

    /**
     * 展位保存
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    public int saveLiteratureChild(LiteratureChild literatureChild){
        try{
            literatureChild.setSts("1");
            literatureChild.setLiteratureChildCount(0);
            String dateStr = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
            literatureChild.setLiteratureChildTime(dateStr);
            //王硕修改上传为mp3格式时缺少固定图，在PC与小程序展示效果不好
            if(literatureChild.getLiteratureChildImg() == ""){
                literatureChild.setLiteratureChildImg("1558317978219.png");
            }
            literatureChildRepositoty.save(literatureChild);
            return 1;
        }catch(Exception e){
            return -1;
        }
    }

    /**
     * 展位修改
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    public int updateLiteratureChild(LiteratureChild literatureChild){
        try{
            literatureChild.setSts("1");
            String dateStr = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
            literatureChild.setLiteratureChildTime(dateStr);
            literatureChildRepositoty.saveAndFlush(literatureChild);
            return 1;
        }catch(Exception e){
            return -1;
        }
    }

    /**
     * 展位阅读数修改
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    public int updateLiteratureChildForCount(Integer literatureChildId){
        try{
            LiteratureChild literatureChild = this.findOneForLiteratureChild(literatureChildId);
            literatureChild.setLiteratureChildCount(literatureChild.getLiteratureChildCount()+1);
            literatureChildRepositoty.saveAndFlush(literatureChild);
            return 1;
        }catch(Exception e){
            return -1;
        }
    }

    /**
     * 展位findOne
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    public LiteratureChild findOneForLiteratureChild(Integer literatureChildId){
        String sql = "SELECT * FROM `literaturechild` WHERE literature_child_id = '"+literatureChildId+"' AND sts = '1'";
        List<LiteratureChild> literatureChildList = queryUtil.query(sql, LiteratureChild.class);
        if(literatureChildList != null){
            return literatureChildList.get(0);
        }else{
            return null;
        }
    }

    /**
     * 展位删除--逻辑
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    public int deleteLiteratureChild(Integer literatureChildId){
        try{
            LiteratureChild literatureChild = this.findOneForLiteratureChild(literatureChildId);
            literatureChild.setSts("0");
            String dateStr = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
            literatureChild.setLiteratureChildTime(dateStr);
            literatureChildRepositoty.saveAndFlush(literatureChild);
            return 1;
        }catch(Exception e){
            return -1;
        }
    }



    public List<LiteratureChild> findLiteratureChildById(Integer literId){
        return  literatureChildRepositoty.findLiteratureChildById(literId);
    }
}
